package il.ac.tau.cg.ex3.entities.scene.lights;

import il.ac.tau.cg.ex3.entities.styles.Color;
import il.ac.tau.cg.ex3.math.Point;
import il.ac.tau.cg.ex3.math.Vector;

public class PointLight extends Light
{
	private Point position;
	private Vector attenuationVector;

	public PointLight(Color color, Point position)
	{
		this(color, position, new Vector(1.0, 0.0, 0.0));
	}
	
	public PointLight(Color color, Point position, Vector attenuationVector)
	{
		super(color);

		this.position   = position;
		this.attenuationVector = attenuationVector;
	}

	@Override
	public Color getIntensity(Point point)
	{
		double attenuation = getAttenuation(point);

		Color light = new Color(getColor().getR() / attenuation, 
								getColor().getG() / attenuation,
								getColor().getB() / attenuation);

		return light;
	}
	
	@Override
	public Vector getDirection(Point point)
	{
		return new Vector(position, point).normalize();
	}

	public Point getPosition()
	{
		return position;
	}

	public void setPosition(Point position)
	{
		this.position = position;
	}
	
	public Vector getAttenuationVector()
	{
		return attenuationVector;
	}

	public void setAttenuationVector(Vector attenuation)
	{
		this.attenuationVector = attenuation;
	}

	private double getDistance(Point destPoint)
	{
		Vector dir = new Vector(position, destPoint);

		return dir.length();
	}

	private double getAttenuation(Point destPoint)
	{
		double distance = getDistance(destPoint);

		double attentuation = attenuationVector.getX() + attenuationVector.getY() * distance + attenuationVector.getZ() * Math.pow(distance, 2);

		return attentuation;
	}
}
